package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.oss.IOssAttachmentSvc;
import com.kmxd.ams.client.oss.dto.OssAttachmentQueryDTO;
import com.kmxd.ams.client.oss.vo.OssAttachmentListVO;
import com.kmxd.ams.client.system.ISysNoticeSvc;
import com.kmxd.ams.client.system.ISysNoticeTypeSvc;
import com.kmxd.ams.client.system.dto.SysNoticeAddDTO;
import com.kmxd.ams.client.system.dto.SysNoticeEditDTO;
import com.kmxd.ams.client.system.dto.SysNoticeQueryDTO;
import com.kmxd.ams.client.system.dto.SysNoticeTypeQueryDTO;
import com.kmxd.ams.client.system.vo.SysNoticeListVO;
import com.kmxd.ams.client.system.vo.SysNoticeTypeListVO;
import com.kmxd.ams.client.system.vo.SysNoticeVO;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.enums.OssAttachmentTypeEnum;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.exception.BizTipException;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.system.entity.SysNotice;
import com.kmxd.ams.infra.system.mapper.SysNoticeMapper;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告 服务实现类
 *
 * @author ZuoLG
 * @since 2022-07-15
 */
@Service
@AllArgsConstructor
public class SysNoticeSvcImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
    implements ISysNoticeSvc {

  private final ISysNoticeTypeSvc sysNoticeTypeSvc;
  private final IOssAttachmentSvc ossAttachmentSvc;
  private SysNoticeMapper sysNoticeMapper;

  @Override
  public Page<SysNoticeListVO> selectPage(Page reqPage, SysNoticeQueryDTO req) {
    LambdaQueryWrapper<SysNotice> queryWrapper =
        QueryGen.init(new QueryWrapper<SysNotice>(), req).lambda();
    Page<SysNotice> page = sysNoticeMapper.selectPage(reqPage, queryWrapper);
    Page<SysNoticeListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    List<SysNoticeListVO> list = BeanUtil.copyToList(page.getRecords(), SysNoticeListVO.class);
    if (ObjectUtil.isNotEmpty(list)) {
      list.forEach(
          vo -> {
            // 设置电子文件
            OssAttachmentQueryDTO qry = new OssAttachmentQueryDTO();
            qry.setPid(vo.getId());
            qry.setType(OssAttachmentTypeEnum.NOTICE.getType());
            List<OssAttachmentListVO> ossAttachmentListVOS = ossAttachmentSvc.selectList(qry);
            if (Objects.nonNull(ossAttachmentListVOS)) {
              vo.setFile(ossAttachmentListVOS);
            }
          });
    }
    voPage.setRecords(list);
    return voPage;
  }

  @Override
  public Page<SysNoticeListVO> selectUserPage(Page reqPage, SysNoticeQueryDTO req) {

    List<SysNoticeListVO> list =
        sysNoticeMapper.selectUserPage(req, reqPage.offset(), reqPage.getSize());
    Long total = sysNoticeMapper.selectUserPageCount(req);
    Page<SysNoticeListVO> res = new Page<>(reqPage.getCurrent(), reqPage.getSize());
    if (ObjectUtil.isNotEmpty(list)) {
      list.forEach(
          vo -> {
            // 设置电子文件
            OssAttachmentQueryDTO qry = new OssAttachmentQueryDTO();
            qry.setPid(vo.getId());
            qry.setType(OssAttachmentTypeEnum.NOTICE.getType());
            List<OssAttachmentListVO> ossAttachmentListVOS = ossAttachmentSvc.selectList(qry);
            if (Objects.nonNull(ossAttachmentListVOS)) {
              vo.setFile(ossAttachmentListVOS);
            }
          });
    }
    res.setRecords(list);
    res.setTotal(total);
    return res;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(SysNoticeAddDTO req) {
    if (ObjectUtil.isNotEmpty(req.getStartTime()) || ObjectUtil.isNotEmpty(req.getEndTime())) {
      if (!ObjectUtil.isAllNotEmpty(req.getStartTime(), req.getEndTime())) {
        throw BizTipException.instance(ErrorCode.VALIDATE_ERROR, "开始及结束时间全设置或全不设置！");
      }
    }

    if (ObjectUtil.isEmpty(req.getStartTime()) || ObjectUtil.isEmpty(req.getEndTime())) {
      if (!ObjectUtil.isAllEmpty(req.getStartTime(), req.getEndTime())) {
        throw BizTipException.instance(ErrorCode.VALIDATE_ERROR, "开始及结束时间全设置或全不设置！");
      }
    }
    SysNotice entity = BeanUtil.copyProperties(req, SysNotice.class);

    if (Objects.nonNull(entity.getStartTime())) {
      Date startTime = entity.getStartTime();
      entity.setStartTime(DateUtil.beginOfDay(startTime));
    }
    if (Objects.nonNull(entity.getEndTime())) {
      Date endTime = entity.getEndTime();
      entity.setEndTime(DateUtil.endOfDay(endTime).offset(DateField.MILLISECOND, -999));
    }
    entity.setComId(SecurityUtil.getCropId());
    sysNoticeMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(SysNoticeEditDTO req) {
    if (ObjectUtil.isNotEmpty(req.getStartTime()) || ObjectUtil.isNotEmpty(req.getEndTime())) {
      if (!ObjectUtil.isAllNotEmpty(req.getStartTime(), req.getEndTime())) {
        throw BizTipException.instance(ErrorCode.VALIDATE_ERROR, "开始及结束时间全设置或全不设置！");
      }
    }

    if (ObjectUtil.isEmpty(req.getStartTime()) || ObjectUtil.isEmpty(req.getEndTime())) {
      if (!ObjectUtil.isAllEmpty(req.getStartTime(), req.getEndTime())) {
        throw BizTipException.instance(ErrorCode.VALIDATE_ERROR, "开始及结束时间全设置或全不设置！");
      }
    }
    SysNotice entity = BeanUtil.copyProperties(req, SysNotice.class);
    if (Objects.nonNull(entity.getStartTime())) {
      Date startTime = entity.getStartTime();
      entity.setStartTime(DateUtil.beginOfDay(startTime));
    }
    if (Objects.nonNull(entity.getEndTime())) {
      Date endTime = entity.getEndTime();
      entity.setEndTime(DateUtil.endOfDay(endTime).offset(DateField.MILLISECOND, -999));
    }
    sysNoticeMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    sysNoticeMapper.deleteBatchIds(idList);
  }

  @Override
  public SysNoticeVO view(Long id) {
    SysNotice entity = sysNoticeMapper.selectById(id);
    SysNoticeVO vo = BeanUtil.copyProperties(entity, SysNoticeVO.class);
    if (Objects.isNull(vo)) {
      return vo;
    }
    // 设置电子文件
    OssAttachmentQueryDTO qry = new OssAttachmentQueryDTO();
    qry.setPid(vo.getId());
    qry.setType(OssAttachmentTypeEnum.NOTICE.getType());
    List<OssAttachmentListVO> ossAttachmentListVOS = ossAttachmentSvc.selectList(qry);
    if (Objects.nonNull(ossAttachmentListVOS)) {
      vo.setFile(ossAttachmentListVOS);
    }
    return vo;
  }

  @Override
  public List<SysNoticeTypeListVO> selectUserNoticeType(SysNoticeTypeQueryDTO req) {
    return sysNoticeTypeSvc.selectList(req);
  }
}
